﻿using AutoMapper;
using EFEntity;
using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Threading.Tasks;
using System.Linq;
using Iservices;
using Microsoft.Data.SqlClient;
using System.Data;
using Microsoft.EntityFrameworkCore;

namespace Services
{
    public class salary_standardServices : Isalary_standardServices
    {
        private readonly MyDbContext myDbContext;

        public salary_standardServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;

        }

        public async Task<List<salary_standard>> salary_standardFenYe(FenYe fy)
        {
            SqlParameter[] ps = {
            new SqlParameter(){  ParameterName="@pageSize",Value=fy.PageSize},
            new SqlParameter(){  ParameterName="@keyName",Value="ssd_id"},
            new SqlParameter(){  ParameterName="@tabelName",Value="salary_standard"},
            new SqlParameter(){  ParameterName="@where",Value=fy.Where},
            new SqlParameter(){  ParameterName="@currentPage",Value=fy.CurrentPage},           
            new SqlParameter(){  ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            new SqlParameter(){  ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            };
            List<Esalary_standard> list = await myDbContext.salary_standard.FromSqlRaw("exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage,@rows out, @pages out", ps).ToListAsync();
            fy.Rows = (int)ps[5].Value;
            fy.Pages = (int)ps[6].Value;
            //把EO-DTO
            List<salary_standard> list2 = new List<salary_standard>();
            foreach (Esalary_standard item in list)
            {
                salary_standard t = new salary_standard()
                {
                    ssd_id = item.ssd_id,
                    standard_id = item.standard_id,
                    standard_name = item.standard_name,
                    designer = item.designer,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    salary_sum = item.salary_sum,
                    check_status = item.check_status,
                    change_status = item.change_status,
                    check_comment = item.check_comment,
                    remark = item.remark
                };
                list2.Add(t);
            }
            return list2;
        }
        public async Task<List<salary_standard>> salary_standardFenYe2(FenYe fy)
        {
            SqlParameter[] ps = {
            new SqlParameter(){  ParameterName="@pageSize",Value=fy.PageSize},
            new SqlParameter(){  ParameterName="@keyName",Value="ssd_id"},
            new SqlParameter(){  ParameterName="@tabelName",Value="salary_standard"},
            new SqlParameter(){  ParameterName="@where",Value=fy.Where},
            new SqlParameter(){  ParameterName="@currentPage",Value=fy.CurrentPage},
            new SqlParameter(){  ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            new SqlParameter(){  ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            };
            List<Esalary_standard> list = await myDbContext.salary_standard.FromSqlRaw("exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage,@rows out, @pages out", ps).ToListAsync();
            fy.Rows = (int)ps[5].Value;
            fy.Pages = (int)ps[6].Value;
            //把EO-DTO
            List<salary_standard> list2 = new List<salary_standard>();
            foreach (Esalary_standard item in list)
            {
                salary_standard t = new salary_standard()
                {
                    ssd_id = item.ssd_id,
                    standard_id = item.standard_id,
                    standard_name = item.standard_name,
                    designer = item.designer,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    salary_sum = item.salary_sum,
                    check_status = item.check_status,
                    change_status = item.change_status,
                    check_comment = item.check_comment,
                    remark = item.remark
                };
                list2.Add(t);
            }
            return list2;
        }


        public async Task<List<salary_standard>> salary_standardSelectFenYe2(FenYe fy)
        {
            //  var data = myDbContext.salary_standard.OrderBy(e => e.ssd_id).ToList();
            var data = myDbContext.salary_standard.OrderBy(e => e.ssd_id).Where(e => e.check_status == 1).ToList();

            fy.Rows = data.Count();
            fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
            List<Esalary_standard> list = new List<Esalary_standard>();
            await Task.Run(() =>
            {
                list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
                    .Take(fy.PageSize)
                    .ToList();
            });
            //把EO-DTO
            List<salary_standard> list2 = new List<salary_standard>();
            foreach (Esalary_standard item in list)
            {
                salary_standard t = new salary_standard()
                {
                    ssd_id = item.ssd_id,
                    standard_id = item.standard_id,
                    standard_name = item.standard_name,
                    designer = item.designer,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    salary_sum = item.salary_sum,
                    check_status = item.check_status,
                    change_status = item.change_status,
                    check_comment = item.check_comment,
                    remark = item.remark
                };
                list2.Add(t);
            }
            return list2;
        }
     
        //根据id查询信息
        public async Task<salary_standard> GetSelId(string id)
        {
            Esalary_standard ss = new Esalary_standard();
            await Task.Run(() =>
            {
                ss = myDbContext.salary_standard.Where(e => e.standard_id == id).FirstOrDefault();
            });
            salary_standard s = new salary_standard()
            {
                ssd_id = ss.ssd_id,
                standard_id = ss.standard_id,
                standard_name = ss.standard_name,
                designer = ss.designer,
                register = ss.register,
                checker = ss.checker,
                changer = ss.changer,
                regist_time = ss.regist_time,
                check_time = ss.check_time,
                change_time = ss.change_time,
                salary_sum = ss.salary_sum,
                check_status = ss.check_status,
                change_status = ss.change_status,
                check_comment = ss.check_comment,
                remark = ss.remark
            };
            return s;
        }

        //查看全部
        public async Task<List<salary_standard>> Select()
        {
            List<Esalary_standard> list = new List<Esalary_standard>();
            await Task.Run(() =>
            {
                list = myDbContext.salary_standard.ToList();
            });
            List<salary_standard> list2 = new List<salary_standard>();
            foreach (Esalary_standard item in list)
            {
                salary_standard ss = new salary_standard()
                {
                    ssd_id = item.ssd_id,
                    changer = item.changer,
                    change_status = item.change_status,
                    change_time = item.change_time,
                    checker = item.checker,
                    check_comment = item.check_comment,
                    check_status = item.check_status,
                    check_time = item.check_time,
                    designer = item.designer,
                    register = item.register,
                    regist_time = item.regist_time,
                    remark = item.remark,
                    salary_sum = item.salary_sum,
                    standard_id = item.standard_id,
                    standard_name = item.standard_name,
                };
                list2.Add(ss);
            }
            return list2;
        }
    }
}
